<template>
  <div>
    <div class="search">
      <input
        class="input"
        type="text"
        placeholder="搜索你想要的的商品"
        placeholder-style="color:#cccccc"
        v-model="filterVal"
        confirm-type="search"
        @confirm="handleSearch"
      />
      <icon
        @click="handleSearch"
        class="searchClick"
        type="search"
        size="20"
        color="#cccccc"
      />
      <p class="addClick" @click="addClick">+</p>
    </div>
    <div class="list">
      <div class="tabDiv">
        <p
          v-for="(item,index) in tabList"
          :key="index"
          :class="item.isClick ? 'tabCheck' : 'tabUncheck'"
          @click="tabClick(item)"
        >{{item.label}}</p>
      </div>
      <scroll-view
        class="scroll-wrap no-scroll-bar"
        style="height:calc(100%)"
        scroll-y
        @scrolltolower="handleLoadMore"
      >
        <div v-for="(item,idx) in list" :key="idx" class="goods-block">
          <img :src="item.goodsImg" mode="aspectFit" class="preview" />
          <div class="goods-info">
            <div class="top">
              <p class="goods-name ellipsis">{{item.goodsName}}</p>
              <div class="goodsMo">
                <span class="discount">{{item.goodsDiscount}}</span>
                <p class="goods-stock">库存: {{item.goodsStock}}</p>
              </div>
            </div>
            <div class="bottom">
              <span class="discount">佣金：{{item.disAmount}}</span>
            </div>
          </div>
          <div class="edit-block">
            <button
              class="edit-btn"
              size="mini"
              @click="handleEditGoods(item)"
            >删除</button>
          </div>
        </div>
        <p v-if="alreadyLastPage" class="end-tip">没有更多了</p>
      </scroll-view>
    </div>
    <div class="alertBox" v-if="isAlert">
      <div class="cont">
        <div class="imgBox">
          <img
            src="https://akk.028wkf.cn/kdb-weapp-img/new-kdb-xcx/icon/scts1.png"
            mode="widthFix"
            class="showImg"
          />
        </div>
        <div class="hint">
          <p>提示</p>
          <span>是否确认删除此商品</span>
        </div>
        <button class="btn" @click="handleAffirm">确认</button>
        <div class="close" @click="closeAlert">
          <icon type="clear" size="28" color="#fff" />
        </div>
      </div>
      <div class="zz"></div>
    </div>
  </div>
</template>

<script>
import {} from '@/utils'
import { FIND_SHOP_GOODS_PAGE, UPDATE_SHOP_GOODS_INFO } from '@/api'

export default {
  components: {},
  props: {},
  data () {
    return {
      isAlert: false,
      filterVal: '',
      goodsList: [],
      currentPage: 1,
      alreadyLastPage: false,
      isState: '0',
      goodForm: {},
      tabList: [
        {
          label: '审核中',
          key: '0',
          isClick: true
        },
        {
          label: '审核通过',
          key: '1',
          isClick: false
        }
      ]
    }
  },
  computed: {
    list () {
      return this.goodsList.map(_ => ({
        goodsAmount: '￥' + (_.goodsAmount * 1).toFixed(2),
        disAmount: '￥' + (_.disAmount * 1).toFixed(2),
        goodsDiscount: '￥' + (_.goodsDiscount * 1).toFixed(2),
        goodsName: _.goodsName,
        goodsNo: _.goodsNo,
        goodsStock: _.goodsStock,
        goodsImg: _.goodsImg,
        goodsKind: _.goodsKind,
        goodsCost: '￥' + (_.goodsCost * 1).toFixed(2)
      }))
    }
  },
  watch: {},
  created () {},
  mounted () {},
  onReady () {},
  onLoad () {},
  onShow () {
    this.$cookie.remove('describe-img1')
    this.$cookie.remove('describe-img2')
    this.goodsList = []
    this.currentPage = 1
    this.getGoodsList()
  },
  methods: {
    addClick () {
      this.$router.push('/pages/shop-management/supply/add/main')
    },
    tabClick (item) {
      const vm = this
      vm.tabList.forEach(_ => {
        _.isClick = false
      })
      item.isClick = true
      vm.isState = item.key
      vm.currentPage = 1
      vm.getGoodsList()
    },
    handleSearch () {
      this.currentPage = 1
      this.getGoodsList()
    },
    handleLoadMore () {
      if (!this.alreadyLastPage) {
        this.getGoodsList()
      }
    },
    handleEditGoods (item) {
      const vm = this
      vm.isAlert = true
      vm.goodForm = item
    },
    closeAlert () {
      const vm = this
      vm.isAlert = false
      vm.goodForm = {}
    },
    handleAffirm () {
      this.deleteDisShop()
    },
    getGoodsList () {
      const vm = this
      let params = {
        shopId: vm.$cookie.get('account').shopId,
        isDis: 'Y',
        state: vm.isState
      }
      if (vm.filterVal) {
        if (/^\d*$/.test(vm.filterVal) && vm.filterVal.length === 18) {
          params.goodsNo = vm.filterVal
        } else {
          params.goodsName = vm.filterVal
        }
      }
      FIND_SHOP_GOODS_PAGE(params, vm.currentPage, 10)
        .then(r => {
          if (r.data.code === '0') {
            let { list, isFirstPage, isLastPage } = r.data.data
            vm.printList(list, isFirstPage, isLastPage)
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },
    printList (list, isFirst, isLast) {
      if (isFirst) {
        this.goodsList = list
        if (isLast || !list.length) {
          // only one page
          this.alreadyLastPage = true
        } else {
          // has next page
          this.alreadyLastPage = false
          this.currentPage += 1
        }
      } else if (isLast) {
        // more than one page and this is the last page
        this.goodsList.push(...list)
        this.alreadyLastPage = true
      } else {
        // middle page
        this.goodsList.push(...list)
        this.alreadyLastPage = false
        this.currentPage += 1
      }
    },
    deleteDisShop () {
      const vm = this
      let params = {}
      params.goodsNo = vm.goodForm.goodsNo
      params.isDis = 'N'
      UPDATE_SHOP_GOODS_INFO(params)
        .then(r => {
          if (r.data.code === '0') {
            // vm.$alert('删除供应商品成功', '').then(() => {
            vm.goodsList = []
            vm.currentPage = 1
            vm.closeAlert()
            vm.getGoodsList()
            // })
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'static/styles/color.scss';

.search {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 70px;
  position: relative;
  .input {
    width: 200px;
    height: 30px;
    padding-left: 55px;
    font-size: 14px;
    color: #cccccc;
    border: 1px solid #cccccc;
    border-radius: 25px;
  }
  .searchClick {
    position: absolute;
    left: 80px;
    top: 50%;
    transform: translate(0, -50%);
    z-index: 5;
  }
  .addClick {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translate(0, -50%);
    z-index: 6;
    font-size: 30px;
  }
}
.list {
  height: calc(100% - 110px);
  .tabDiv {
    display: flex;
    width: 230px;
    height: 30px;
    margin: auto;
    border-radius: 25px;
    border: 1px solid #767676;
    p {
      width: 50%;
      font-size: 15px;
      line-height: 30px;
      text-align: center;
      border-radius: 25px;
    }
    .tabCheck {
      color: #fff;
      background: linear-gradient(to right, $new-dark-blue, $new-light-blue);
    }
    .tabUncheck {
      color: #000;
      background: #fff;
    }
  }
  > .scroll-wrap {
    .goods-block {
      width: calc(100% - 20px);
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 15px 0;
      margin: 0 10px 10px;
      // box-shadow: 0 0 8px lighten($blue, 20%);
      height: 80px;
      position: relative;
      overflow: hidden;
      transform: translateY(10px);
      > .preview {
        width: 70px;
        height: 70px;
      }
      > .goods-info {
        position: relative;
        font-size: 14px;
        width: calc(100% - 150px);
        height: 70px;
        .top {
          position: absolute;
          top: 0;
          font-size: 13px;
          > .goodsMo {
            display: flex;
            .goods-stock {
              margin-left: 15px;
              color: #767676;
            }
          }
        }
        .bottom {
          position: absolute;
          bottom: 0;
          > .discount {
            display: inline-block;
            color: $red;
            min-width: 50px;
            padding-right: 10px;
          }
        }
      }
      > .edit-block {
        display: flex;
        align-items: center;
        height: 70px;
        > .edit-btn {
          width: 40px;
          border-radius: 25px;
          display: block;
          color: $blue;
          border: 1px solid $blue;
          font-size: 10px;
          background-color: #fff;
          padding: 0;
          height: 20px;
          line-height: 20px;
          transform: translateY(5px);
        }
      }
    }
    .end-tip {
      margin: 0;
      padding: 15px;
      text-align: center;
      color: $gray;
      font-size: 15px;
    }
  }
}
.alertBox {
  .cont {
    width: 320px;
    height: 360px;
    border: 2px solid #eee;
    border-radius: 6px;
    background: #fff;
    position: fixed;
    z-index: 21;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    .imgBox {
      width: 100%;
      height: 210px;
      display: flex;
      align-items: center;
      justify-content: center;
      .showImg {
        width: 180px;
        height: 180px;
      }
    }
    .hint {
      p {
        font-size: 18px;
        color: #000;
        text-align: center;
        line-height: 25px;
      }
      span {
        font-size: 14px;
        color: #b6b6b6;
        text-align: center;
        line-height: 25px;
      }
    }
    .btn {
      width: 80%;
      height: 40px;
      line-height: 40px;
      border-radius: 25px;
      margin: auto;
      color: #fff;
      background: linear-gradient(to right, $new-dark-blue, $new-light-blue);
    }
    .close {
      position: absolute;
      z-index: 22;
      left: 50%;
      bottom: -20%;
      transform: translate(-50%, 0%);
    }
  }
  .zz {
    position: fixed;
    z-index: 20;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-color: #000000;
    opacity: 0.3;
  }
}
</style>
